package com.park.web.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.client.model.question.QuestionModel;
import com.park.client.model.question.QuestionAnswerModel;
import com.park.client.service.question.QuestionAnswerService;
import com.park.client.service.question.QuestionService;
import com.park.common.util.Page;
import com.park.common.util.PageData;
import com.park.common.util.Result;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;

@Controller
@RequestMapping("/admin/question")
public class AdminQuestionController extends BaseController{

	@Autowired
	private QuestionService questionService;
	@Autowired
	private QuestionAnswerService questionAnswerService;
	/**
	 * 问答页面
	 * @return
	 */
	@RequestMapping("questionList")
	public String questionList(HttpServletRequest request){
			return Global.ADMIN_PATH+"question/questionList";
	}
	
	@RequestMapping("listRows")
	@ResponseBody
	public PageData<QuestionModel> listRows(Integer rows,@RequestParam(name="page",defaultValue="1")Integer pageNow,HttpServletRequest request){
		PageData<QuestionModel>  pageData = new PageData<>();
		QuestionModel question = new QuestionModel();
		Integer total = questionService.selectTotal(question);
		Page page = new Page(pageNow,total,rows);
		question.setStartPos(page.getStartPos());
		question.setPageSize(page.getPageSize());
		List<QuestionModel> list = questionService.selectPagination(question);
		pageData.setTotal(total);
		pageData.setRows(list);
		return pageData;
	}

	@RequestMapping("save")
	@ResponseBody
	public Result<Boolean> save(QuestionModel question,HttpServletRequest request){
		Result<Boolean> result = new Result<>(true);
		boolean success = questionService.insertSelective(question);
		if(success){
			result.setMessage("添加成功!");
		}else{
			result.setMessage("添加失败!");
		}
		result.setSuccess(success);
		return result;
	}
	
	@RequestMapping("deleteById")
	@ResponseBody
	public Result<Boolean> deleteById(@RequestParam("id") Long id,HttpServletRequest request){
		Result<Boolean> result = new Result<>(true);
		boolean success = questionService.deleteByPrimaryKey(id);
		if(success){
			result.setMessage("删除成功!");
		}else{
			result.setMessage("删除失败!");
		}
		result.setSuccess(success);
		return result;
	}
	
	@RequestMapping("edit")
	@ResponseBody
	public Result<Boolean> edit(QuestionModel question,HttpServletRequest request){
		Result<Boolean> result = new Result<>(true);
		boolean success = questionService.updateByPrimaryKeySelective(question);
		if(success){
			result.setMessage("修改成功!");
		}else{
			result.setMessage("修改失败!");
		}
		result.setSuccess(success);
		return result;
	}
	
	/**
	 * 根据问题ID查看回答
	 */
	@RequestMapping("listCommentsByQuestionId")
	@ResponseBody
	public PageData<QuestionAnswerModel> listCommentsByQuestionId(@RequestParam(name="page",defaultValue="1")Integer pageNow,Long questionId,Integer rows){
		PageData<QuestionAnswerModel> pageData = new PageData<>();
		QuestionAnswerModel questionAnswer = new QuestionAnswerModel();
		questionAnswer.setQuestionId(questionId);
		Integer total = questionAnswerService.selectTotal(questionAnswer);
		Page page = new Page(pageNow,total,rows);
		questionAnswer.setStartPos(page.getStartPos());
		questionAnswer.setPageSize(page.getPageSize());
		List<QuestionAnswerModel> list = questionAnswerService.selectPagination(questionAnswer);
		pageData.setTotal(total);
		pageData.setRows(list);
		return pageData;
	}
	
	
}
